﻿using Nop.Core;
using Nop.Core.Data;
using Nop.Core.Domain.Customers;
using Nop.Core.Domain.WindAlert;
using Nop.Services.Events;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Nop.Services.WindAlert
{
    /// <summary>
    /// Spot service
    /// </summary>
    public partial class MobileDeviceService : IMobileDeviceService
    {
        #region Fields

        private readonly IRepository<MobileDevice> _mobileDeviceRepository;
        private readonly IRepository<Customer> _customerRepository;
        private readonly IWorkContext _workContext;
        private readonly IEventPublisher _eventPublisher;
        #endregion

        #region Ctor

        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="cacheManager">Cache manager</param>
        /// <param name="spotRepository">Live Spot repository</param>
        public MobileDeviceService(
            IRepository<MobileDevice> mobileDeviceRepository,
            IRepository<Customer> customerRepository,
            IWorkContext workContext,
            IStoreContext storeContext,
            IEventPublisher eventPublisher)
        {
            this._mobileDeviceRepository = mobileDeviceRepository;
            this._customerRepository = customerRepository;
            this._workContext = workContext;
            _eventPublisher = eventPublisher;
        }
        #endregion

        #region MobileDevice

        /// <summary>
        /// Deletes a MobileDevice
        /// </summary>
        /// <param name="MobileDevice">MobileDevice</param>
        public virtual void DeleteMobileDevice(MobileDevice mobileDevice)
        {
            if (mobileDevice == null)
                throw new ArgumentNullException("mobileDevice");

            _mobileDeviceRepository.Delete(mobileDevice);


            //event notification
            _eventPublisher.EntityDeleted(mobileDevice);
        }

        /// <summary>
        /// Gets a MobileDevice collection
        /// </summary>
        /// <param name="GCMRegistrationToken">gCMRegistrationToken identifier</param>
        /// <returns> MobileDevices collection</returns>
        public MobileDevice GetMobileDeviceByGCMRegistrationToken(string gCMRegistrationToken)
        {
            if (string.IsNullOrEmpty (gCMRegistrationToken))
                return null;

            var query = from md in _mobileDeviceRepository.Table
                        where md.GCMRegistrationToken == gCMRegistrationToken
                        select md;
            var result = query.FirstOrDefault();

            return result;
        }


        /// <summary>
        /// Gets a MobileDevice collection
        /// </summary>
        /// <param name="customerId">Product identifier</param>
        /// <returns> MobileDevices collection</returns>
        public virtual IList<MobileDevice> GetMobileDevicesByCustomerId(int customerId)
        {
            if (customerId == 0)
                return new List<MobileDevice>();

            var query = from md in _mobileDeviceRepository.Table
                        where md.CustomerId == customerId
                        orderby md.Id
                        select md;

            var allMobileDevices = query.ToList();
            var result = new List<MobileDevice>();

            //no filtering
            result.AddRange(allMobileDevices);
            return result;
        }

        /// <summary>
        /// Gets a MobileDevice collection
        /// </summary>
        /// <param name="customerId">customer id identifier</param>
        /// <param name="operationSystem">operationSystem identifier</param>
        /// <param name="operationSystemId">operationSystem id identifier</param>
        /// <returns> MobileDevice collection</returns>
        public virtual MobileDevice GetMobileDevicesByCustomerIdBySystem(int customerId, string operationSystem, string operationSystemId)
        {
            if (customerId == 0)
                return null;

            var query = from md in _mobileDeviceRepository.Table
                        where md.CustomerId == customerId && md.OperationSystem == operationSystem && md.OperationSystemId == operationSystemId
                        orderby md.Id
                        select md;

            return query.FirstOrDefault();
    
        }


        /// <summary>
        /// Gets all MobileDevices
        /// </summary>
        /// <returns>MobileDevice collection</returns>
        public virtual IList<MobileDevice> GetAllMobileDevices()
        {
                var query = from m in _mobileDeviceRepository.Table
                            orderby m.Id
                            select m;
                return query.ToList();
        }

        /// <summary>
        /// Gets all mobileDevice
        /// </summary>
        /// <param name="pageIndex">Page index</param>
        /// <param name="pageSize">Page size</param>
        /// <returns>Cameras</returns>
        public virtual IPagedList<MobileDevice> GetAllMobileDevices(int pageIndex, int pageSize)
        {
            var mobileDevices = GetAllMobileDevices();
            return new PagedList<MobileDevice>(mobileDevices, pageIndex, pageSize);
        }

        /// <summary>
        /// Gets a MobileDevice
        /// </summary>
        /// <param name="MobileDeviceId">MobileDevice identifier</param>
        /// <returns>MobileDevice</returns>
        public virtual MobileDevice GetMobileDeviceById(int mobileDeviceId)
        {
            if (mobileDeviceId == 0)
                return null;

            return _mobileDeviceRepository.GetById(mobileDeviceId);
        }

        /// <summary>
        /// Inserts MobileDevice
        /// </summary>
        /// <param name="MobileDevice">MobileDevice</param>
        public virtual void InsertMobileDevice(MobileDevice mobileDevice)
        {
            if (mobileDevice == null)
                throw new ArgumentNullException("mobileDevice");

            _mobileDeviceRepository.Insert(mobileDevice);

            //event notification
            _eventPublisher.EntityInserted(mobileDevice);
        }

        /// <summary>
        /// Updates the MobileDevice
        /// </summary>
        /// <param name="MobileDevice">MobileDevice</param>
        public virtual void UpdateMobileDevice(MobileDevice mobileDevice)
        {
            if (mobileDevice == null)
                throw new ArgumentNullException("MobileDevice");

            _mobileDeviceRepository.Update(mobileDevice);

            //event notification
            _eventPublisher.EntityUpdated(mobileDevice);
        }

        /// <summary>
        /// Get chaeck existing record
        /// </summary>
        /// <returns>MobileDevice</returns>
        public virtual MobileDevice CheckIfMobileDeviceExists(MobileDevice mobileDevice)
        {
            var query = from md in _mobileDeviceRepository.Table
                        orderby md.Id
                        where md.CustomerId == mobileDevice.CustomerId
                        select md;
            var results = query.FirstOrDefault();
            return results;
        }


        #endregion
     
    }
}
